import java.io.IOException;
import java.util.List;

import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smackx.pubsub.AccessModel;
import org.jivesoftware.smackx.pubsub.ConfigureForm;
import org.jivesoftware.smackx.pubsub.FormType;
import org.jivesoftware.smackx.pubsub.Item;
import org.jivesoftware.smackx.pubsub.ItemPublishEvent;
import org.jivesoftware.smackx.pubsub.LeafNode;
import org.jivesoftware.smackx.pubsub.NodeType;
import org.jivesoftware.smackx.pubsub.PayloadItem;
import org.jivesoftware.smackx.pubsub.PubSubManager;
import org.jivesoftware.smackx.pubsub.PublishModel;
import org.jivesoftware.smackx.pubsub.SimplePayload;
import org.jivesoftware.smackx.pubsub.Subscription;
import org.jivesoftware.smackx.pubsub.listener.ItemEventListener;

/*
 * N. Dumont, TAI Lab
 *
 * Class Test created on 18 mai 2011
 *
 * Copyright (c) 2009 Thales Communications
 * 160 bd de Valmy - BP82 92704 COLOMBES cedex - France
 * All rights reserved.
 *
 */

/**
 * @author tai
 * 
 */
public class Test {
	public void start() {

		ConnectionConfiguration config = new ConnectionConfiguration("192.168.20.12", 5222);
		XMPPConnection connection = new XMPPConnection(config);
		try {
			connection.connect();
			connection.login("nicolas", "nicolas");
		} catch (XMPPException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		// ConfigureForm form = new ConfigureForm(FormType.submit);
		// form.setPersistentItems(false);
		// form.setDeliverPayloads(true);
		// form.setAccessModel(AccessModel.open);
		// form.setPersistentItems(false);

		// PubSubManager creation
		PubSubManager manager = new PubSubManager(connection);

		// Node creation
		LeafNode myNode = null;
		try {
			myNode = (LeafNode) manager.getNode("TestNode");
			
		} catch (Exception l_e) {
			if (l_e.getMessage().contains("item-not-found")) {
				try {
					myNode = (LeafNode) manager.createNode("TestNode",getNodeConfiguration());
				} catch (XMPPException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				System.out.println("New node created.");
			}
		}

		try {
			// Required to recieve the events being published
			myNode.addItemEventListener(new ItemEventListener<Item>() {
				public void handlePublishedItems(ItemPublishEvent<Item> arg0) {
					System.out.println("item received:" + arg0);
					PayloadItem p = (PayloadItem) arg0.getItems().get(0);
					System.out.println(p.toXML());
				}
			});

			List<Subscription> subs = myNode.getSubscriptions();
			for (Subscription l_subscription : subs) {
				System.out.println(l_subscription);
				
			}

			System.in.read();

			// Publish item
			SimplePayload payload = new SimplePayload("data", null,
					"<data type='int' value='15'/>");
			PayloadItem<SimplePayload> item = new PayloadItem<SimplePayload>(payload);

			myNode.send(item);
			myNode.send(item);
			myNode.send(item);

		} catch (XMPPException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		try {
			System.in.read();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		connection.disconnect();

	}
	
	private ConfigureForm getNodeConfiguration(){
		ConfigureForm form = new ConfigureForm(FormType.submit);
	      form.setAccessModel(AccessModel.open);
	      form.setPublishModel(PublishModel.open);
	      
	      form.setDeliverPayloads(true);	      
	      form.setPersistentItems(false);
	      
	      form.setPresenceBasedDelivery(false);
	      
	      form.setNotifyConfig(false);
	      form.setNotifyDelete(false);
	      form.setNotifyRetract(false);
	      
	      form.setNodeType(NodeType.leaf);
	      form.setSubscribe(true);	      
	      
	      return form;
	}
}
